/*
 * Golazo MA4
 * Copyright (C) 2008 Golazo MASHUP AWARD 4th team.
 * http://code.google.com/p/golazo/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License 
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 * existsUser
 */ 
function existsUser(userId) {
  
  // 会員IDが期待どおりか
  if (!Golazo.User.checkUserId(userId))
    return false;

  // 会員を検索する
  return !!Golazo.User.find(userId);
};

/**
 * trustUser
 */
function trustUser(userId) {
  var session = Jaxer.session;

  // 私は誰か
  var me = Golazo.User.find(session.get('me'));
  if (!me)
	throw new Error('(me)');

  // 会員IDは期待どおりか
  if (!Golazo.User.checkUserId(userId))
    throw new Error('(userId)');

  // 会員を検索する
  var user = Golazo.User.find(userId);
  if (!user)
	throw new Error('(user)');

  // 会員を信頼する
  try {
    me.trust(user);
  } catch (e) {
	throw new Error(''+e);
  }
};

/**
 * distrustUser
 */
function distrustUser(userId) {
  var session = Jaxer.session;
  
  // 私は誰か
  var me = Golazo.User.find(session.get('me'));
  if (!me)
	throw new Error('(me)');

  // 会員IDは期待どおりか
  if (!Golazo.User.checkUserId(userId))
    throw new Error('(userId)');

  // 会員を検索する
  var user = Golazo.User.find(userId);
  if (!user)
	throw new Error('(user)');

  // 会員を信頼しない
  try {
    me.distrust(user);
  } catch (e) {
	throw new Error(''+e);
  }
};

/**
 * addAction
 */ 
function addAction(note) {
  var session = Jaxer.session;
  
  // 私は誰か
  var me = Golazo.User.find(session.get('me'));
  if (!me)
	throw new Error('(me)');

  // メモは期待どおりか
  if (!Golazo.Action.checkNote(note))
    throw new Error('(note)');

  // メモから位置情報を抽出する
  var kinds = [];
  try {
    kinds = new Golazo.Kinds.get(note);
  } catch (e) {
    Jaxer.Log.warn('The remote procedure call failed.', e);
  }

  var where = Golazo.findAll(kinds, function(kind) {
    return kind.latitude && kind.longitude /*&& where.altitude*/;
  })[0] || Golazo.WHERE_TOKYO;

  var action;
  try {
	
	// メモを保存する
	action = new Golazo.Action(me);
    action.note = note;
    action.latitude = where.latitude; 
    action.longitude = where.longitude;
    action.altitude = where.altitude;
	action.save();
	
	// メモを購読する
	action.subscribe(me);
  
  } catch (e) {
	throw new Error(''+e);
  }

  me.email = '';

  // メモの購読者と作成者
  action.subscribers = [me];
  action.creator = me;
  
  // 編集、削除できる
  action.editable = true;
  action.deletable = true;

  return action;
};

/**
 * deleteAction
 */
function deleteAction(actionId) {
  var session = Jaxer.session;

  // 私は誰か
  var me = Golazo.User.find(session.get('me'));
  if (!me)
	throw new Error('(me)');

  // メモIDは期待どおりか
  if (!Golazo.Action.checkActionId(actionId))
    throw new Error('(actionId)');
  
  // メモを検索する
  var action = Golazo.Action.find(actionId);
  if (action) {

    // 私のメモか
    if (me.equals(action.creatorId)) {

      // メモを削除する
	  try {
        action.delete();
      } catch (e) {
	    throw new Error(''+e);
      }
	
	// 購読者かどうか
	} else {

      // メモの購読を中止する
	  try {
        action.unsubscribe(me);
      } catch (e) {
	    throw new Error(''+e);
      }
	}
  }
};
